package com.zlx.store.common.exception.handler;


import com.zlx.store.common.exception.*;
import com.zlx.store.common.results.HttpResp;
import com.zlx.store.common.constant.RespCode;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.Date;

/**
 * 优雅的解决Exception问题
 */
@RestControllerAdvice
public class UserExceptionHandler {

    @ExceptionHandler(UsernameIsEmptyException.class)
    public HttpResp usernameIsEmptyHandler(UsernameIsEmptyException e) {
        return new HttpResp(RespCode.USER_NAME_REGISTER_ISNULL_ERROR, e.getMessage(), new Date());
    }

    @ExceptionHandler(UsernameNotFoundException.class)
    public HttpResp userNameNotFound(UsernameNotFoundException e) {
        return new HttpResp(RespCode.USER_LOGIN_ERROR_USERNAME_NO_FIND, e.getMessage(), new Date());
    }

    @ExceptionHandler(UsernameOrPasswordErrorException.class)
    public HttpResp usernameOrPasswordError(UsernameOrPasswordErrorException e) {
        return new HttpResp(RespCode.USER_LOGIN_ERROR_PASSWORD_OR_USERNAME, e.getMessage(), new Date());
    }

    @ExceptionHandler(UserNoLoginException.class)
    public HttpResp userNotLogin(UserNoLoginException e) {
        return new HttpResp(RespCode.USER_LOGIN_ERROR_NO_LOGIN, e.getMessage(), new Date());
    }

    @ExceptionHandler(IllegalUserException.class)
    public HttpResp illegalUserException(IllegalUserException e) {
        return new HttpResp(RespCode.USER_LOGIN_ERROR_NO_ROLE, e.getMessage(), new Date());
    }

    @ExceptionHandler(TokenHasExpiredException.class)
    public HttpResp tokenExpiredException(TokenHasExpiredException e) {
        return new HttpResp(RespCode.USER_LOGIN_ERROR_JWT_EXPIRE, e.getMessage(), new Date());
    }

    @ExceptionHandler(GoodsStockEmptyException.class)
    public HttpResp goodsStockEmptyException(GoodsStockEmptyException e) {
        return new HttpResp<>(RespCode.GOODS_STOCK_EMPTY, e.getMessage(), new Date());
    }
    @ExceptionHandler(ShoppingCartEmptyException.class)
    public HttpResp shoppingCartEmptyException(ShoppingCartEmptyException e){
        return new HttpResp<>(RespCode.GOODS_STOCK_EMPTY,e.getMessage(),new Date());
    }

}
